package redis

import (
	"context"
	"fmt"
	"srv/tiktok"
	"strconv"
)

func MailAdd(mailId, mailNum, userId int, title, size string, price float64) error {
	key := fmt.Sprintf("cart:user_Id_%d:mail_Id_%d", userId, mailId)
	return Rdb.HMSet(context.Background(), key, map[string]interface{}{
		"mailId":  mailId,
		"userId":  1,
		"title":   title,
		"size":    size,
		"price":   price,
		"mailNum": mailNum,
	}).Err()
}
func CheckMail(mailId int, userId int) bool {
	key := fmt.Sprintf("cart:%d:%d", userId, mailId)
	return Rdb.Exists(context.Background(), key).Val() == 1
}
func UpdateMail(mailId int, userId int, num int) error {
	key := fmt.Sprintf("cart:%d:%d", userId, mailId)
	return Rdb.HSet(context.Background(), key, "num", num).Err()
}
func MailList(userId int64) (tiktokList []*tiktok.TiktokList, err error) {
	key := fmt.Sprintf("cart:mail_Id_%d", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		mailInfo := Rdb.HGetAll(context.Background(), s).Val()
		mailId, _ := strconv.Atoi(mailInfo["mailId"])
		mailNum, _ := strconv.Atoi(mailInfo["mailNum"])
		userId, _ := strconv.Atoi(mailInfo["userId"])
		title := mailInfo["title"]
		size := mailInfo["size"]
		price, _ := strconv.ParseFloat(mailInfo["price"], 64)
	}
	tiktokList = append(tiktokList, &tiktok.TiktokList{
		UserId: userId,
		MailId: s,
		Num:    0,
		Title:  "",
		Size:   "",
		Price:  0,
	})
}
